﻿<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
<mapper namespace="${Package}.dao.${table.TableNameAlias}Dao">
	<resultMap type="${Package}.domain.${table.TableNameAlias}" id="${table.TableNameAlias}Map">
	#foreach($c in $ColumnList)
		#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
		<result column="${c.ColumnName}" property="${Utils.ToTitleLower(${c.ColumnNameAlias})}" />
		#else
			<id column="${c.ColumnName}" property="${Utils.ToTitleLower(${c.ColumnNameAlias})}" />
		#end
	#end 
	</resultMap>
	<select id="countAll" resultType="int">
		select count(0) t from ${table.TableName}
	</select>
	<select id="countAllForPage" parameterType="java.util.Map"
		resultType="int">
		select count(0) t from ${table.TableName}
		<where>
			<if test="status!=null">
				and status=#{status}
			</if>
			<if test="title">
			and Enum_Name like "%"#{title}"%" 
			</if>
		</where>
	</select>
	<select id="selectAllByPage" resultMap="${table.TableNameAlias}Map">
		select * from ${table.TableName} 
		<where>
			<if test="status!=null">
				and status=#{status}
			</if>
			<if test="title">
			and Enum_Name like "%"#{title}"%" 
			</if>
		</where>  order by sort desc,$primaryKey desc
	</select>
	<select id="selectAll" resultMap="${table.TableNameAlias}Map">
		select * from ${table.TableName}  order by sort desc,$primaryKey desc
	</select>
	<select id="selectForWhere" resultMap="${table.TableNameAlias}Map">
		select * from ${table.TableName} t  order by sort desc,$primaryKey desc
	</select>
	<select id="selectOne" parameterType="int" resultMap="${table.TableNameAlias}Map">
		select *
		from ${table.TableName} t where
		$primaryKey=#{id}
	</select>
	<insert id="insert" useGeneratedKeys="true" keyProperty="$primaryKey"
		parameterType="${Package}.domain.${table.TableNameAlias}">
		insert into ${table.TableName}
		 <trim prefix="(" suffix=")" suffixOverrides=",">  
				#foreach($c in $ColumnList)
				   #if(${c.ColumnNameAlias}!=$primaryKeyAlias)
				    $c.ColumnName,
				   #end
				#end 
        </trim>  
		values
		<trim prefix="(" suffix=")" suffixOverrides=",">  
			#foreach($c in $ColumnList)
				#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
					#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
					#{$columnName},
				#end
			#end 
        </trim> 
	</insert>
	<update id="update" parameterType="${Package}.domain.${table.TableNameAlias}">
		update ${table.TableName} 
		<set>
		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
			#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
			<if test="$columnName!=null">
			     ${c.ColumnName}=#{$columnName},
				 </if>
            #end
		#end
		</set>
		where $primaryKey=#{${Utils.ToTitleLower($primaryKeyAlias)}}
	</update>
	<delete id="delete" parameterType="int">
		delete from ${table.TableName} where
		$primaryKey=#{id}
	</delete>
	<delete id="deleteForInIds" parameterType="java.lang.String">
		delete from ${table.TableName} where $primaryKey in
		<foreach item="idItem" collection="array" open="(" separator=","
			close=")">
			#{idItem}
		</foreach>
	</delete>
	<update id="audit" parameterType="java.util.Map">
		update ${table.TableName} set status=#{status} where $primaryKey in
		<foreach item="idItem" collection="idArr" open="(" separator=","
			close=")">
			#{idItem}
		</foreach>
	</update>
</mapper>
